jQuery(document).ready(function($) {
    var hasChked=false;
    $("select[name='prodname']").on('change', function(){
        var salerid = $(this).find("option:selected").val();
        $("ul.prod_info").find('li').hide();
        $("li#li_"+salerid).show();
    });

    $("input[name='certnum']").blur(function(){
        if( $(this).val().length==0 ) {
            $("table.detailTbl").hide();
            return;
        } else if( isNaN($(this).val()) ){
            $("table.detailTbl").show();
            $("span#msgShow").text('凭证号必须为数字！').removeClass('ok').addClass('error');
            return false;
        } else if( !hasChked ){
            $("table.detailTbl").hide();
            $("span#msgShow").empty().removeClass('error');
        }
    });
    var chkNumber=function(){
	
        var salerid = $("select[name='prodname']").find("option:selected").val(),
            orderStr = '',
            certnum = $("input[name='certnum']").val();
        if( salerid =='') {
            alert('请先选择产品！');
            return false;
        }
        if($("input[name='certnum']").val()=='' || isNaN($("input[name='certnum']").val())){
            return false;
        }
		// 管理员验证时间
		chktime = $("#chk_time").val();

        $.ajax({
            url:'call/jh_prod_dt.php',
            data:{certnum:certnum,salerid:salerid,chktime:chktime,action:'TerminalChk'},
            dataType:'json',
            type:'post',
            beforeSend:function(){
                $("span.wait").css('display','block');
                $("tbody#tbody").empty();
            },
            success:function(data){
                if(data.status!='ok') {
                    $("span#msgShow").text(data.msg).removeClass('ok').addClass('error');
                } else{
                    hasChked = true;
                    var orderData = data.body;
                    $("span#msgShow").text('验证成功！').removeClass('error').addClass('ok');
                    orderStr+='<tr><th>订单号：</th><td>'+orderData.UUordernum+'</td><th>流水号：</th><td>'+orderData.uuino+'</td></tr>';
                    orderStr+='<tr><th>取票人：</th><td>'+orderData.UUordername+'</td><th>联系号码：</th><td>'+orderData.UUordertel+'</td></tr>';
                    orderStr+='<tr><th>下单时间：</th><td>'+orderData.UUordertime+'</td><th>出游时间：</th><td>'+orderData.UUbegintime+'</td></tr>';
                    orderStr += '<tr><td colspan="4" class="sep"></td></tr>' +
                        '<tr class="tickets_list"><th colspan="2">票名称</th><th colspan="2">票数</th></tr>';
                    for(var key in orderData.tickets) {
//                        console.log(ticket);
                        orderStr += '<tr class="tickets_list"><td colspan="2">'+orderData.tickets[key].title+'</td><td  colspan="2">'+orderData.tickets[key].tnum+'</td></tr>';
                    }
                    $("tbody#tbody").html(orderStr);
                }
            },
            complete:function(){
                $("span.wait").css('display','none');
                $("table.detailTbl").show();
            }
        });
    };
    $("#checkSubmitBtn").on('click',chkNumber);
    $("input#certnum").bind('keydown',function(e){
        var key = e.which;
            if (key == 13) {
            e.preventDefault();
            $("#checkSubmitBtn").click();
        }
    });
});
function sortObject(data) {
    var sorted = {},key, a=[];
    for(key in data) {
        if(data.hasOwnProperty(key)) {
            a.push(key);
        }
    }
    a.sort();
    for(key=0; key< a.length; key++) {
        sorted[a[key]] = data[a[key]];
    }
    return sorted;
}
function sendCode(){
	$.post("call/jh_sendVerify.php",{action:"sendTerVerify"},function(data,textState){
		alert(data.reqMsg);
	},"json");
}

function terCodeVerify(){
	$code = $("#ter_code").val();
	if($code==""){
		alert("请输入验证码");
		return false;
	}
	$.post("call/jh_sendVerify.php",{action:"verifyCode",terCode:$code},function(data,textState){
		alert(data.reqMsg);
	},"json");
}
